from tkinter_ import *
from tkinter_ import ttk
import requests

url_0 = r"http://172.30.4.129/a70.htm"
url_post = r"http://172.30.4.129/a70.htm?isReback=1"
url_host = r"http://172.30.4.129/"

operator_list = (('移动', "@cmcc"), ('联通', '@unicom '), ('电信', '@telecom'))
post_information = {
    '0MKKey': '123456',
    'buttonClicked': '',
    'cmd': '',
    'DDDDD': '用户名@运营商代码',
    'err_flag': '',
    'Login': '',
    'para': '00',
    'password': '',
    'R1': '0',
    'R2': '',
    'R3': '0',
    'R6': '0',
    'redirect_url': '',
    'upass': '密码',
    'user': '',
    'username': ''}


def initialize():
    """已登录返回：1，否则返回：0"""
    url = requests.get(url_host)
    url.encoding = "gbk"
    url = url.text
    BOOL = re.search("注销页", url, )
    if BOOL is not None:
        print("yidenglu")
        return 1
    else:
        print("meidenglu")
        return 0


def write_users(x, i):
    s = "\t".join(x) + '\t' + i + "\n"
    print(s)
    with open(r"./TK_UserData.ini", mode='w', encoding='utf-8') as f:
        f.write(s)
    return quit()


def wifi_find():
    wifi = pywifi.PyWiFi()
    card = wifi.interfaces()[0]
    card.scan()
    wifi_list = card.scan_results()
    for x in wifi_list:
        print(x.ssid)
        if x.ssid == "Huas-Student":
            return 1
    return 0


def wifi_interlinkage():
    wifi = pywifi.PyWiFi()  # 创建一个对象
    card = wifi.interfaces()[0]  # 选取一个网卡
    print(card.name())  # 输出网卡名称
    card.disconnect()  # 断开现有连接
    time.sleep(1)

    profile = pywifi.Profile()
    profile.ssid = "Huas-Student"
    card.connect(card.add_network_profile(profile))

    return 0


def initialize():
    """已登录返回：1，否则返回：0"""
    url = requests.get(url_host)
    url.encoding = "gbk"
    url = url.text
    BOOL = re.search("注销页", url, )
    if BOOL is not None:
        return 1
    else:
        return 0


def get_user_password():
    """从文件把用户名和密码变成列表方便遍历 """
    L = []
    with open(r"./TK_UserData.ini", mode='r', encoding='utf-8') as user:
        for x in user:
            x = x.split("\t")
            list_ = x.pop()
            x.append(list_.split("\n")[0])
            L.append(x)
        return L


def gui_tk():
    root = Tk()
    root.title("校园网登录程序")

    Label(root, text='用户名').grid(row=1, column=0)
    user_name = StringVar()
    Entry(root, width=30, textvariable=user_name).grid(row=1, column=1, sticky=W)

    Label(root, text='密码').grid(row=2, column=0)
    password = StringVar()
    Entry(root, width=30, textvariable=password).grid(row=2, column=1, sticky=W)

    Label(root, text='运营商').grid(row=3, column=0)
    operation = StringVar()
    numberChosen = ttk.Combobox(root, width=12, textvariable=operation,)
    numberChosen['values'] = ('移动\t@cmcc', '联通\t@unicom', '电信\t@telecom')  # 设置下拉列表的值
    numberChosen.grid(row=3, column=1)  # 设置其在界面中出现的位置  column代表列   row 代表行
    numberChosen.current(0)  # 设置下拉列表默认显示的值，0为 numberChosen['values'] 的下标值

    Button(root, text='登录', width=12, command=lambda: write_users([user_name.get(), password.get()], numberChosen.get())).grid(row=6, column=0)
    Button(root, text='取消', width=12, command=lambda: root.quit()).grid(row=6, column=1)

    root.mainloop()
    return 0

if __name__ == "__main__":
    try:
        get_user_password()
    except:
        gui_tk()

    print("jieshu")
